<template>
  <div class='container'>
  <el-card>
    <!-- 头部 -->
    <el-row>
      <el-col :span="22">
        <span class="sp">学科名称</span>
        <el-input v-model="obj.subjectName" placeholder="请输入学科名称" style="margin-right: 10px;"></el-input>
        <el-button type="primary" size="small" @click="clr">清除</el-button>
        <el-button type="success" size="small" @click="searchfn">搜索</el-button>
      </el-col>
      <el-col :span="2">
        <el-button type="success" class="btn" size="small" icon="el-icon-edit" @click="addfn()">新增学科</el-button>
      </el-col>
    </el-row>
      <el-alert
      :title="`数据一共 ${counts} 条`"
      type="info"
      show-icon
      :closable="false">
  </el-alert>
   <!-- 头部 -->

   <!-- 主体 -->
    <template>
    <el-table
      :data="tableData"
      :header-cell-style="{background:'#fafafa','border-bottom':'3px solid #e8e8e8'}"
      style="width: 100%">
      <el-table-column
        label="序号"
        align="center"
        type="index">
      </el-table-column>
      <el-table-column
      align="center"
        prop="subjectName"
        label="学科名称">
      </el-table-column>
      <el-table-column
      align="center"
        prop="username"
        label="创建者">
      </el-table-column>
      <el-table-column
      align="center"
        label="创建日期">
        <template slot-scope="scope">
          {{scope.row.addDate|addDate}}
        </template>
      </el-table-column>
      <el-table-column
      align="center"
        label="前台是否显示">
        <template slot-scope="scope">
          {{scope.row.isFrontDisplay ===1?'是':'否'}}
        </template>
      </el-table-column>
      <el-table-column
      align="center"
        prop="twoLevelDirectory"
        label="二级目录">
      </el-table-column>
      <el-table-column
      align="center"
        prop="tags"
        label="标签">
      </el-table-column>
      <el-table-column
      align="center"
        prop="totals"
        label="题目数量">
      </el-table-column>
      <el-table-column
        label="操作"
        width="250">
        <template slot-scope="scope">
          <el-button type="text" @click="addclk(scope.row)">学科分类</el-button>
          <el-button type="text" @click="addclkfn(scope.row)">学科标签</el-button>
          <el-button type="text" @click="revampfn(scope.row)">修改</el-button>
          <el-button type="text" @click="delfn(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </template>
  <!-- 分页 -->
  <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page=obj.page
      background
      :page-sizes="[5, 10, 20, 50]"
      :page-size=obj.pagesize
      layout="prev, pager, next, sizes,jumper"
      :total="counts">
    </el-pagination>
   <!-- 主体 -->
  </el-card>

  <!-- 弹框 -->
  <el-dialog
      :title="isades?'修改学科':'新增学科'"
      :visible.sync="dialogVisible"
      v-if="dialogVisible"
      width="20%">
  <subjectsAdd  :row="row" :isades="isades" :dialogVisible.sync="dialogVisible" @success=success></subjectsAdd>
  </el-dialog>
   <!-- 弹框 -->
  </div>
</template>

<script>
import { list, remove } from '@/api/hmmm/subjects'
import subjectsAdd from '../components/subjects-add.vue'
export default {
  components: {
    subjectsAdd
  },
  data () {
    return {
      tableData: [],
      dialogVisible: false,
      obj: {
        page: 1,
        pagesize: 10,
        subjectName: ''
      },
      counts: 0,
      isades: false,
      row: {}
    }
  },
  methods: {
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.obj.pagesize = val
      this.getupdate()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.obj.page = val
      this.getupdate()
    },
    // 获取数据列表
    async getupdate () {
      const res = await list(this.obj)
      // console.log(res.data)
      this.tableData = res.data.items
      this.counts = res.data.counts
    },
    // 点击操作中的删除
    delfn (id) {
      console.log(id)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        console.log(12)
        await remove(id, { id })
        if (this.tableData.length === 1) {
          this.obj.page--
          if (this.obj.page === 1) {
            this.obj.page = 1
          }
        }
        this.getupdate()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 点击清除按钮
    clr () {
      this.obj.subjectName = ''
      this.getupdate()
    },
    // 点击搜索按钮
    searchfn () {
      this.getupdate()
    },
    // 点击新增学科
    addfn () {
      // console.log(this.dialogVisible)
      this.isades = false
      this.dialogVisible = true
    },
    // 点击操作中的修改
    revampfn (row) {
      // console.log(row, 5666)
      this.row = row
      this.isades = true
      this.dialogVisible = true
    },
    // 子向父传值
    success () {
      this.getupdate()
      this.dialogVisible = false
    },
    // 跳转学科分类页面
    addclk (row) {
      this.$router.push({ path: 'directorys', query: { id: row.id, subjectName: row.subjectName }, name: 'subjects-directorys', params: { row } })
    },
    // 跳转学科标签页面
    addclkfn (row) {
      this.$router.push({ path: 'tags', query: { id: row.id, subjectName: row.subjectName }, name: 'subjects-tags', params: { row } })
    }
  },
  created () {
    this.getupdate()
  }
}
</script>

<style scoped lang='scss'>
.container{
 margin: 10px 15px;
 .el-input{
  width: 200px;
  }
  .btn{
    margin: 0px 20px;
  }
  .sp{
    margin-right: 10px;
  }
  .el-alert{
    margin-top: 10px;
    margin-bottom: 20px;
  }
  .el-pagination{
    text-align: right;
    margin-top: 20px;
  }
}

</style>
